package example.dawangba.com.z09315222_exp3;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Spinner spinner;
    private EditText editNum1;
    private EditText editNum2;
    private TextView result;
    private Button button;
    private String[] huansaun = {"+","-","*","/"};
    String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = (Spinner) findViewById(R.id.spinner);
        editNum1 = (EditText) findViewById(R.id.numEdit);
        editNum2 = (EditText) findViewById(R.id.numEdit2);
        button = (Button) findViewById(R.id.jisuan);
        result = (TextView) findViewById(R.id.result);

        ArrayAdapter<String> huansuan_adapter = new ArrayAdapter<String>(
                this,android.R.layout.simple_spinner_dropdown_item,huansaun
        );
        spinner.setAdapter(huansuan_adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                flag = spinner.getSelectedItem().toString();
                Log.e("Dawangba", String.valueOf(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag.equals("+")){
                    AddListener addListener = new AddListener();
                    addListener.onClick(v);
                }

                if (flag.equals("-")){
                    SubListener subListener = new SubListener();
                    subListener.onClick(v);
                }
                if (flag.equals("*")){
                    MulListener mulListener = new MulListener();
                    mulListener.onClick(v);
                }
                if (flag.equals("/")){
                    DivListener divListener = new DivListener();
                    divListener.onClick(v);
                }
            }
        });
    }

    private void showDialog(){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("错误");
        dialog.setMessage("输入数字无效!");
        dialog.setPositiveButton("确定",null);
        dialog.show();
    }

    //加法操作
    private class AddListener implements View.OnClickListener {

        public void onClick(View v) {

            if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){
                showDialog();
            } else {
                float number1 = Float.parseFloat((editNum1.getText().toString()));
                float number2 = Float.parseFloat((editNum2.getText().toString()));
                result.setText(number1 + number2 + "");
            }
        }
    }

    //减法操作
    private class SubListener implements View.OnClickListener {
        public void onClick(View v) {
            if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){
                showDialog();
            } else {
                float number1 = Float.parseFloat((editNum1.getText().toString()));
                float number2 = Float.parseFloat((editNum2.getText().toString()));
                result.setText(number1 - number2 + "");
            }
        }
    }
    //乘法操作
    private class MulListener implements View.OnClickListener {
        public void onClick(View v) {
            if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){
                showDialog();
            } else {
                float number1 = Float.parseFloat((editNum1.getText().toString()));
                float number2 = Float.parseFloat((editNum2.getText().toString()));
                result.setText(number1 * number2 + "");
            }
        }
    }
    //除法操作
    private class DivListener implements View.OnClickListener {
        public void onClick(View v) {
            if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){
                showDialog();
            } else {
                float number1 = Float.parseFloat((editNum1.getText().toString()));
                float number2 = Float.parseFloat((editNum2.getText().toString()));
                if (number2 == 0){
                    showDialog();
                } else {
                    result.setText(number1 / number2 + "");
                }
            }
        }
    }
}
